/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.util.List;
import model.*;
import org.hibernate.HibernateException;
import org.hibernate.PropertyValueException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * TODO: Make a short day
 *
 */
public class ElevHandler {

    private Session session;

    public ElevHandler(Session session) {
        this.session = session;
    }

    public Elev[] hentElever() throws HibernateException {

        String hql = "from Elev WHERE slettet = 0";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Elev[] elever = new Elev[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {

            elever[i] = (Elev) resultList.get(i);
        }
        session.getTransaction().commit();


        return elever;
    }

    public Kontaktperson[] hentKontapersonerPåType(int ktype) throws HibernateException {
        String kt = ktype + "";
        String hql = "FROM Kontaktperson WHERE Ktype = \'" + kt.charAt(0) + "\'";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Kontaktperson[] sagsbehandler = new Kontaktperson[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {

            sagsbehandler[i] = (Kontaktperson) resultList.get(i);
        }
        session.getTransaction().commit();


        return sagsbehandler;
    }

    public void sletLog(ElevLog log) throws HibernateException {
               
        session.beginTransaction();
        String hql = "from ElevLog";
        session.flush();
        session.createQuery(hql); 
        log.getElev().getElevLogs().remove(log);
        log.getPersonale().getElevLogs().remove(log);
        session.delete(log);
        session.getTransaction().commit();
    }

    public void nyLog(ElevLog log) {

        session.beginTransaction();
        String hql = "from ElevLog";
        session.flush();
        session.createQuery(hql);
        log.getElev().getElevLogs().add(log);
        session.save(log);
        session.getTransaction().commit();
        opdaterElev(log.getElev());
    }

    public Kontaktperson[] hentKontaktPersonerElev(Elev elev) throws HibernateException {

        Object[] obArr = elev.getKontaktpersons().toArray();
        Kontaktperson[] elArr = new Kontaktperson[obArr.length];

        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (Kontaktperson) obArr[i];
        }

        return elArr;
    }

    public ElevMedicin[] hentElevMedicin(Elev elev) {
        Object[] obArr = elev.getElevMedicins().toArray();
        ElevMedicin[] elArr = new ElevMedicin[obArr.length];

        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (ElevMedicin) obArr[i];
        }

        return elArr;
    }

    public ElevAllergi[] hentElevAllergi(Elev elev) {
        Object[] obArr = elev.getElevAllergis().toArray();
        ElevAllergi[] elArr = new ElevAllergi[obArr.length];

        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (ElevAllergi) obArr[i];
        }

        return elArr;
    }

    public HoldFag[] hentElevHoldFag(Elev elev) {

        Object[] obArr = elev.getHold().getHoldFags().toArray();
        HoldFag[] fagArr = new HoldFag[obArr.length];

        for (int i = 0; i < obArr.length; i++) {
            fagArr[i] = (HoldFag) obArr[i];
        }

        return fagArr;

    }

    public ElevTelefon[] hentElevTelefon(Elev elev) {

        Object[] obArr = elev.getElevTelefons().toArray();
        ElevTelefon[] elArr = new ElevTelefon[obArr.length];

        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (ElevTelefon) obArr[i];
        }

        return elArr;

    }

    public Kontaktperson[] hentSagsbehandler() throws HibernateException {

        String hql = "FROM Kontaktperson WHERE Ktype = 2";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Kontaktperson[] sagsbehandler = new Kontaktperson[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {

            sagsbehandler[i] = (Kontaktperson) resultList.get(i);
        }
        session.getTransaction().commit();


        return sagsbehandler;
    }

    public void opdaterElev(Elev elev) {

        session.beginTransaction();
        String hql = "from Elev";
        session.createQuery(hql);
        session.flush();
        session.update(elev);
        session.getTransaction().commit();

    }

    public void nyAftale(ElevAftale aftale) {

        session.beginTransaction();
        String hql = "from ElevAftale";
        session.createQuery(hql);
        aftale.getElev().getElevAftales().add(aftale);
        session.save(aftale);
                session.flush();
        session.getTransaction().commit();
        opdaterElev(aftale.getElev());
    }

    public void nyAllergi(ElevAllergi allergi) {

        session.beginTransaction();
        String hql = "from ElevAllergi";session.flush();
        session.createQuery(hql);
        allergi.getElev().getElevAllergis().add(allergi);
        session.save(allergi);
        session.flush();
        session.getTransaction().commit();
        opdaterElev(allergi.getElev());
    }

    public void nyTelefon(ElevTelefon telefon) {

        session.beginTransaction();
        String hql = "from ElevTelefon";
        
        session.createQuery(hql);
        telefon.getElev().getElevTelefons().add(telefon);
        session.save(telefon);
        session.flush();
        session.getTransaction().commit();
        opdaterElev(telefon.getElev());
    }

    public void nyMedicin(ElevMedicin medicin) {

        session.beginTransaction();
        String hql = "from ElevMedicin";
        
        session.createQuery(hql);
        medicin.getElev().getElevMedicins().add(medicin);
        session.flush();
        session.save(medicin);
        session.getTransaction().commit();
        opdaterElev(medicin.getElev());
    }

    public void sletAftale(ElevAftale aftale) throws HibernateException {

        session.beginTransaction();
        String hql = "from ElevAftale";
        session.flush();
        session.createQuery(hql);
        aftale.getElev().getElevAftales().remove(aftale);
        session.delete(aftale);
        session.getTransaction().commit();
        opdaterElev(aftale.getElev());
    }

    public void sletPotentiale(Potentiale potentiale) throws HibernateException {
         session.beginTransaction();
        String hql = "from ElevLog";
        session.flush();
        session.createQuery(hql); 
        potentiale.getElev().getPotentiales().remove(potentiale);
        potentiale.getTrinmaal().getPotentiales().remove(potentiale);
        session.delete(potentiale);
        session.getTransaction().commit();
    }

    public void nytPotentiale(Potentiale pot) {

        session.beginTransaction();
        String hql = "from Potentiale";
        session.flush();
        session.createQuery(hql);
        pot.getElev().getPotentiales().add(pot);
        session.save(pot);
        session.getTransaction().commit();
        opdaterElev(pot.getElev());
    }

    public Evaluering hentEvaluering(Elev elev, Fag fag) throws HibernateException {
        String hql = "from model.Evaluering as E where E.fag.fag = '" + fag.getFag() + "' AND E.elev.cpr = '" + elev.getCpr() + "'";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Evaluering[] eval = new Evaluering[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {
            eval[i] = (Evaluering) resultList.get(i);
        }
        Evaluering evaluering;
        if (eval.length != 0) {
            evaluering = eval[0];
        } else {
            evaluering = new Evaluering(new EvalueringId(elev.getCpr(), null), elev, fag);
        }
        session.getTransaction().commit();
        return evaluering;
    }
    
      public Handleplan hentHandleplan(Elev elev, Fag fag) throws HibernateException {
        String hql = "from model.Handleplan as H where H.fag.fag = '" + fag.getFag() + "' AND H.elev.cpr = '" + elev.getCpr() + "'";
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        Handleplan[] handleplans = new Handleplan[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {
            handleplans[i] = (Handleplan) resultList.get(i);
        }
        Handleplan handleplan;
        if (handleplans.length != 0) {
            handleplan = handleplans[0];
        } else {
            handleplan = new Handleplan(new HandleplanId(elev.getCpr(), null), elev, fag);
        }
        session.getTransaction().commit();
        return handleplan;
    }

    public void nyEvaluering(Evaluering eval) {
        session.beginTransaction();
        String hql = "from Evaluering";
        session.flush();
        session.createQuery(hql);
        session.save(eval);
        session.getTransaction().commit();
    }

    public Postnr hentPostnr(String pnr) {

        String hql = "FROM Postnr where postNummer = \'" + pnr + "\'";
        Postnr p;
        session.beginTransaction();
        Query q = session.createQuery(hql);
        List resultList = q.list();
        if (resultList.size() > 0) {
            p = (Postnr) resultList.get(0);
        } else {
            p = new Postnr("0000", "Ugyldigt Postnummer");
        }
        session.flush();
        session.getTransaction().commit();
        return p;
    }
    
      public void nyHandleplan(Handleplan h){
       session.beginTransaction();
        String hql = "from Handleplan";
        session.flush();
        session.createQuery(hql);
        session.save(h);
        session.getTransaction().commit();  
    }

    public void nyElev(Elev elev) {
        session.beginTransaction();
        String hql = "from Elev";
        session.flush();
        session.createQuery(hql);
        session.save(elev);
        session.flush();
        session.getTransaction().commit();

    }

    public void opdaterEvaluering(Evaluering eval) {
        session.beginTransaction();
        String hql = "from Evaluering";
        session.createQuery(hql);
        session.update(eval);
        session.flush();
        session.getTransaction().commit();

    }

    public void opdaterPotentiale(Potentiale p) {
        session.beginTransaction();
        String hql = "from Potentiale";
        session.createQuery(hql);
        session.flush();
        session.update(p);
        session.getTransaction().commit();

    }
    
 
    
    public void opdaterHandleplan(Handleplan h){
       session.beginTransaction();
        String hql = "from Handleplan";
        session.flush();
        session.createQuery(hql);
        session.update(h);
        session.getTransaction().commit();  
    }
    
    
    public void sletElevMedicin(ElevMedicin em) {
        session.beginTransaction();
        String hql = "from ElevMedicin";
        session.flush();
        session.createQuery(hql); 
        em.getElev().getElevMedicins().remove(em);
        session.delete(em);
        session.getTransaction().commit();
    }

    public void sletElevTelefon(ElevTelefon et) {
          session.beginTransaction();
        String hql = "from ElevTelefon";
        session.flush();
        session.createQuery(hql); 
        et.getElev().getElevTelefons().remove(et);
        session.delete(et);
        session.getTransaction().commit();
    }

    public void sletElevAllergi(ElevAllergi ea) {
          session.beginTransaction();
        String hql = "from ElevAllergi";
        session.flush();
        session.createQuery(hql); 
        ea.getElev().getElevAllergis().remove(ea);
        session.delete(ea);
        session.getTransaction().commit();
    }

    public void opretElev(Elev elev) throws PropertyValueException{
        System.out.println(elev.getElevRekvirskole().getRekvirLeder());    
        
        session.beginTransaction();
        String hql = "from Elev";
        session.createQuery(hql);
        session.flush();
        session.save(elev);
        session.getTransaction().commit();

    }
    
    public void opretRekvirskole(ElevRekvirskole skole) throws PropertyValueException{   
        
        session.beginTransaction();
        String hql = "from ElevRekvirskole";
        session.createQuery(hql);
        session.flush();
        session.save(skole);
        session.getTransaction().commit();

    }

    public ElevLog[] hentElevLog(Elev elev) {
       Object[] obArr = elev.getElevLogs().toArray();
        ElevLog[] elArr = new ElevLog[obArr.length];

        for (int i = 0; i < obArr.length; i++) {
            elArr[i] = (ElevLog) obArr[i];
        }

        return elArr;
  
    }

}
